package com.ttms.web.manage;

import com.google.gson.Gson;
import com.ttms.dao.impl.BaseDao;
import com.ttms.pojo.*;
import com.ttms.service.PlayService;
import com.ttms.service.ScheduleService;
import com.ttms.service.StudioService;
import com.ttms.service.impl.PlayServiceImpl;
import com.ttms.service.impl.ScheduleServiceImpl;
import com.ttms.service.impl.StudioServiceImpl;
import com.ttms.utils.WebUtils;
import com.ttms.web.common.BaseServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

public class ReleaseServlet extends BaseServlet {
    private ScheduleService scheduleService = new ScheduleServiceImpl();
    private PlayService playService = new PlayServiceImpl();
    private StudioService studioService = new StudioServiceImpl();
    private List<Studio> studios = studioService.findStudios();
    private List<Play> plays = playService.queryPlays();
    private List<PlayLang> playLangs = playService.queryPlayLangs();
    private List<PlayType> playTypes = playService.queryPlayTypes();

    /**
     * 处理分页功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Schedule> page = scheduleService.page(pageNo,pageSize);
        page.setUrl("/ttms/manage/releaseServlet?action=page");
        req.setAttribute("studios", studios);
        req.setAttribute("page",page);
        req.setAttribute("plays", plays);
        req.getRequestDispatcher("/front-end/views/manage/releasemanage.jsp").forward(req,resp);
    }

    protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("hello");
        String play_name = req.getParameter("play_name");
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Play> page = playService.queryPlayByName(pageNo,pageSize,play_name);
        String url = "/ttms/manage/releaseServlet?action=pageByName&play_name=" +play_name;
        page.setUrl(url);
        req.setAttribute("page",page);
        req.setAttribute("playLangs", playLangs);
        req.setAttribute("playTypes", playTypes);
        req.getRequestDispatcher("/front-end/views/manage/releasemanage.jsp").forward(req,resp);
    }



    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int play_id = WebUtils.parseInt(req.getParameter("play_id"),0) ;
        Play play = playService.queryPlayById(play_id);
        Schedule schedule = WebUtils.copyParamToBean(req.getParameterMap(),new Schedule());
        BigDecimal play_ticket_price = play.getPlay_ticket_price();
        schedule.setSched_ticket_price(new BigDecimal(String.valueOf(play_ticket_price)));
        scheduleService.addSchedule(schedule);
        resp.sendRedirect("/ttms/manage/releaseServlet?action=page&pageNo="+req.getParameter("pageNo"));
    }


    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("sched_id"), 0);
        System.out.println("删除"+id);
        scheduleService.deleteScheduleById(id);
        resp.sendRedirect("/ttms/manage/releaseServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }



    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int play_id = WebUtils.parseInt(req.getParameter("play_id"),0) ;
        Play play = playService.queryPlayById(play_id);
        BigDecimal play_ticket_price = play.getPlay_ticket_price();
        int id = WebUtils.parseInt(req.getParameter("sched_id"), 0);
        Schedule schedule = WebUtils.copyParamToBean(req.getParameterMap(),new Schedule());
        schedule.setSched_ticket_price(new BigDecimal(String.valueOf(play_ticket_price)));
        scheduleService.updateSchedule(schedule);
        resp.sendRedirect("/ttms/manage/releaseServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }



    protected void getSchedule(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        System.out.println("查询的id为：" + id);
        Schedule schedule = scheduleService.queryScheduleById(id);
        schedule.setSched_id(id);
        Gson gson = new Gson();
        String json = gson.toJson(schedule);
        System.out.println(json);
        resp.getWriter().write(json);
    }




}
